package com.example.smartcity_ikun3.ui.person.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.example.smartcity_ikun3.NewLoginActivity
import com.example.smartcity_ikun3.R
import com.example.smartcity_ikun3.logic.model.LoginAllModel
import com.example.smartcity_ikun3.logic.model.LoginModel
import com.example.smartcity_ikun3.logic.network.RetrofitService
import com.example.smartcity_ikun3.logic.network.RetrofitService.Companion.create
import com.example.smartcity_ikun3.logic.network.SmartApi
import com.example.smartcity_ikun3.logic.util.Util.show
import com.google.gson.JsonObject
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import org.json.JSONObject
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class LoginAllActivity : AppCompatActivity() {

    private lateinit var mPersonName: TextView
    private lateinit var mPersonSex: TextView
    private lateinit var mPersonPhone: TextView
    private lateinit var mPersonUserId: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_login_all)
        title = "个人信息"

        mPersonName = findViewById(R.id.LoginPersonName)
        mPersonSex = findViewById(R.id.LoginPersonSex)
        mPersonPhone = findViewById(R.id.LoginPersonPhone)
        mPersonUserId = findViewById(R.id.LoginPersonUserId)



        // 设置个人信息
        setLoginAll()
        // 修改个人信息
        setNewLogin()
    }

    /**
     * 修改个人信息
     */
    private fun setNewLogin() {
        mPersonName.setOnClickListener {
            startActivity(Intent(this, NewLoginActivity::class.java).apply {
                putExtra("name", mPersonName.text.toString())
            })
        }

        mPersonSex.setOnClickListener {
            startActivity(Intent(this, NewLoginActivity::class.java).apply {
                putExtra("sex", mPersonSex.text.toString())
            })
        }

        mPersonPhone.setOnClickListener {
            startActivity(Intent(this, NewLoginActivity::class.java).apply {
                putExtra("phone", mPersonPhone.text.toString())
            })
        }
    }


    /**
     * 设置个人信息
     */
    private fun setLoginAll() {
        getSharedPreferences("token", Context.MODE_PRIVATE).apply {
            val token = this?.getString("token", "").toString()
            RetrofitService.create<SmartApi>()
                .getLoginAll(token).enqueue(object : Callback<LoginAllModel> {
                override fun onFailure(p0: Call<LoginAllModel>, p1: Throwable) {
                    "获取用户信息失败！".show()
                    Log.d("NET", "onFailure: login --> ${p1.message}")
                }

                override fun onResponse(p0: Call<LoginAllModel>, p1: Response<LoginAllModel>) {
                    val body = p1.body()
                    if (body != null) {
                        if (body.code == 200){
                            mPersonName.text = body.user?.nickName
                            if (body.user?.sex == "0") mPersonSex.text = "男"
                            if (body.user?.sex == "1") mPersonSex.text = "女"
                            mPersonPhone.text = body.user?.phonenumber
                            val id = body.user?.idCard?.substring(0,4) + "*****" + body.user?.idCard?.substring(11)
                            mPersonUserId.text = id
                            mPersonUserId.setOnClickListener {
                                startActivity(Intent(this@LoginAllActivity, NewLoginActivity::class.java).apply {
                                    putExtra("id", body.user?.idCard)
                                })
                            }
                        }else "加载用户信息失败！".show()
                    }
                }

            })
        }
    }


    override fun onStart() {
        super.onStart()
        setLoginAll()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}